package com.ai.planttrees.controller;
import com.ai.planttrees.model.UserTree;
import com.ai.planttrees.service.UserTreeService;
import com.ai.planttrees.util.BaseUtil;
import com.ai.planttrees.util.DES;
import com.ai.planttrees.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
* Created by CodeGenerator on 2018/04/23.
*/
@Api(value = "用户活动接口",tags = {"活动基本信息管理"})
@RestController
@RequestMapping("/tree")
public class UserTreeController {
    private Logger logger = LoggerFactory.getLogger(UserTreeController.class);
    @Resource
    private UserTreeService userTreeService;


    @ApiOperation(value = "初始化用户活动信息-->0次种树，流量为0" ,notes = "返回结果")
    @PutMapping("/user")
    public Result initUserTree(@ApiParam("用户主键") String phone){
        UserTree userTree = new UserTree();
        userTree.setId(BaseUtil.getUUID());
        userTree.setPhone(phone);
        userTree.setFlow(0L);
        userTree.setTreeNum(0);
        int i = userTreeService.save(userTree);
        return Result.success(i);
    }

    @ApiOperation(value = "查询用户活动信息" , notes = "返回结果")
    @PostMapping("/user")
    public Result selUserInfo(@ApiParam("用户主键") @RequestParam("phone") String phone){
        return userTreeService.selUserInfo(phone);
    }

    @ApiOperation(value = "查询用户活动兑奖信息" , notes = "返回结果")
    @PostMapping("/user/prize")
    public Result selUserInfoPrize(@ApiParam("用户主键") @RequestParam("phone") String phone){
        logger.info("按外部用户Id查询用户活动信息，参数：{}",phone);
        UserTree userTree = userTreeService.findBy("phone",phone);
        logger.info("结果：{}",userTree);
        if(userTree==null){
            return Result.fail("未找到指定用户");
        }
        return userTreeService.selUserInfoPrize(userTree);
    }

}
